package com.prj.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.prj.dto.MenuDTO;
import com.prj.entity.Menu;
import com.prj.service.MenuService;
import com.prj.vo.MenuVO;
import com.prj.vo.Result;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "菜单权限管理")
@RestController
@RequestMapping("menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @Operation(summary = "查询所有菜单权限")
    @GetMapping("list")
    public Result<List<MenuVO>> list() {
        List<MenuVO> menuVOS = new ArrayList<>();
        List<Menu> menus = menuService.list();
        for (Menu menu : menus) {
            MenuVO menuVO = new MenuVO(menu.getId(), menu.getName(), menu.getAcl(), menu.getParentId(), menu.getLevel());
            menuVOS.add(menuVO);
        }
        return Result.success(buildMenuTree(menuVOS));
    }

    @Operation(summary = "新增/修改菜单权限信息")
    @PostMapping("createOrEdit")
    public Result<Boolean> createOrEdit(@RequestBody MenuDTO menuDTO) {
        Menu menu = BeanUtil.copyProperties(menuDTO, Menu.class);
        return Result.success(menuService.saveOrUpdate(menu));
    }

    @Operation(summary = "删除菜单权限信息")
    @DeleteMapping("remove/{id}")
    public Result<Boolean> remove(@Parameter(description = "菜单权限id")
                                  @PathVariable Integer id) {
        List<Integer> menuIds = new ArrayList<>();
        cascadeDeleteMenu(menuIds, id);
        menuIds.add(id);
        menuService.removeBatchByIds(menuIds);
        return Result.success(true);
    }

    public void cascadeDeleteMenu(List<Integer> menuIds, int menuId) {
        List<Menu> childMenus = menuService.list(new LambdaQueryWrapper<Menu>().eq(Menu::getParentId, menuId));
        for (Menu childMenu : childMenus) {
            menuIds.add(childMenu.getId());
            cascadeDeleteMenu(menuIds, childMenu.getId());
        }
    }

    @Operation(summary = "根据id获取菜单权限信息")
    @GetMapping("getById/{id}")
    public Result<MenuVO> getById(@Parameter(description = "菜单权限id")
                                  @PathVariable Integer id) {
        Menu menu = menuService.getById(id);
        return Result.success(BeanUtil.copyProperties(menu, MenuVO.class));
    }

    private List<MenuVO> buildMenuTree(List<MenuVO> menuVOS) {
        Map<Integer, MenuVO> menuVOMap = new HashMap<>();
        List<MenuVO> result = new ArrayList<>();
        for (MenuVO menuVO : menuVOS) {
            menuVOMap.put(menuVO.getId(), menuVO);
        }
        for (MenuVO menuVO : menuVOS) {
            int parentId = menuVO.getParentId();
            if (parentId == 0) {
                result.add(menuVO);
            } else {
                MenuVO parent = menuVOMap.get(parentId);
                if (parent != null) {
                    parent.addChild(menuVO);
                }
            }
        }
        return result;
    }
}
